﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class StockTakeDetailBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public StockTakeDetailBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string StoreID, string StockTakeID)
        {
            if (DAO.T_STOCKTAKE_DETAILs.Where(t => t.STOREID.ToString() == StoreID
                                                && t.STOCKTAKE_ID.ToString() == StockTakeID).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_STOCKTAKE_DETAIL> GetStockTakeDetailList()
        {
            var list = DAO.T_STOCKTAKE_DETAILs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list = list.OrderBy(OrderCondition);
            }

            return list;
        }

        public T_STOCKTAKE_DETAIL GetStockTakeDetailByID(string ID)
        {
            return DAO.T_STOCKTAKE_DETAILs.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }

        public int AddStockTakeDetail(T_STOCKTAKE_DETAIL StockTakeDetail)
        {
            try
            {
                StockTakeDetail.ID = DAO.GetGUID();

                DAO.T_STOCKTAKE_DETAILs.InsertOnSubmit(StockTakeDetail);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddStockTakeDetail(T_STOCKTAKE_DETAIL StockTakeDetail, out string StockTakeDetailGuid)
        {
            try
            {
                StockTakeDetail.ID = DAO.GetGUID();
                
                DAO.T_STOCKTAKE_DETAILs.InsertOnSubmit(StockTakeDetail);
                DAO.SubmitChanges();

                StockTakeDetailGuid = StockTakeDetail.ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                StockTakeDetailGuid = "";
                return -1;
            }
        }

        public int UpdateStockTakeDetail(T_STOCKTAKE_DETAIL StockTakeDetail)
        {
            try
            {
                T_STOCKTAKE_DETAIL updateEntity = DAO.T_STOCKTAKE_DETAILs.SingleOrDefault(t=>t.ID.ToString()==StockTakeDetail.ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.GROSS_WEIGHT = StockTakeDetail.GROSS_WEIGHT;
                updateEntity.NET_WEIGHT = StockTakeDetail.NET_WEIGHT;
                updateEntity.OVERAGE = StockTakeDetail.OVERAGE;
                updateEntity.PACKET_QTY = StockTakeDetail.PACKET_QTY;
                updateEntity.PACKET_QTY2 = StockTakeDetail.PACKET_QTY2;
                updateEntity.STOCKTAKE_ID = StockTakeDetail.STOCKTAKE_ID;
                updateEntity.STOREID = StockTakeDetail.STOREID;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteStockTakeDetail(string ID)
        {
            try
            {
                T_STOCKTAKE_DETAIL deleteEntity = DAO.T_STOCKTAKE_DETAILs.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_STOCKTAKE_DETAILs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteStockTakeDetails(List<string> IDs)
        {
            try
            {
                foreach (string ID in IDs)
                {
                    T_STOCKTAKE_DETAIL deleteEntity = DAO.T_STOCKTAKE_DETAILs.SingleOrDefault(t => t.ID.ToString() == ID);
                    if (deleteEntity == null)
                        return -1;
                    DAO.T_STOCKTAKE_DETAILs.DeleteOnSubmit(deleteEntity);
                }

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
